package com.jindong.dailylearning.example.controller;

import com.alibaba.excel.EasyExcel;
import com.jindong.dailylearning.example.service.ExampleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/example")
@Api(value = "接口示例（字段说明）",tags = "接口示例（表示说明）")
public class ExampleController {
    @Autowired
    ExampleService exampleService;

    @Resource
    ExampleService exampleService2;

    @ApiOperation(value = "RestFul风格接口")
    @GetMapping("/getRest/{name}")
    public String getName(@PathVariable String name){
        return exampleService.getHello(name);
    }

    @ApiOperation(value = "普通接口")
    @GetMapping("/getCommon")
    public String getCommon(@RequestParam("name") String name){
        return exampleService2.getHello2(name);
    }

    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date=df.format(new Date());
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("分类模板"+date, "UTF-8");

        String projectPath = System.getProperty("user.dir")+"/src/main/resources/";

        // 这里 需要指定写用哪个class去读，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
//        List<ExcelVo> allSubjectToExcel = eduSubjectService.getAllSubjectToExcel();
//        EasyExcel.write(projectPath+fileName, ExcelVo.class).sheet("模板").doWrite(allSubjectToExcel);
        List<String> strings = Arrays.asList("we", "are", "family", "bnfsuidvbn", "hsbfv");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), String.class).sheet("模板").doWrite(strings);
    }

}
